/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 苏李涛
 * Date: 2024-06-20
 * Time: 20:41
 */

/**
 * 通过构造对象，把交换的值放到堆区，并指向对应的
 */
class My_Val{
    public int val;
}

class Ture{
    public static void swap2(My_Val my_val1, My_Val my_val2) {
        int tmp = my_val1.val;
        my_val1.val = my_val2.val;
        my_val2.val = tmp;
    }

    public static void main(String[] args) {
        My_Val my_val1 = new My_Val();
        My_Val my_val2 = new My_Val();

        my_val1.val = 10;
        my_val2.val = 20;

        System.out.println("交换前"+ my_val1.val+ " "+ my_val2.val);

        swap2(my_val1,my_val2);
        System.out.println("交换后"+ my_val1.val+ " "+ my_val2.val);
    }
}









public class Test {


    /**
     * 错误方法，只是形参交换了
     * @param x
     * @param y
     */

    public void swap(int x, int y) {
        int tmp = x;
        x = y;
        y = tmp;
    }
    public static void main(String[] args) {
        Test test = new Test();
        int a = 10;
        int b = 20;
        System.out.println("交换前"+ a+ " "+ b);

        test.swap(a, b);
        System.out.println("交换后"+ a+ " "+ b);
    }
}
